﻿using System.Collections.Generic;
using System.Linq;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Validators
{
    public class EachTaskShouldBeEstimated : IValidator
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            foreach (var entity in context.Metrics.Tasks)
            {
                var devAssign = entity.Assignments.FirstOrDefault(x => context.Metrics.IsDeveloper(x.GeneralUser));
                if (entity.Effort< 1)
                {
                    if (devAssign != null && !context.ActiveEmployees.Contains(devAssign.GeneralUser.ToString())) continue;
                    yield return new ValidationResult(
                        string.Format("{0}: {1} should have effort, state: {2} {3}", entity.EntityType.Name, entity.Id, entity.EntityState, GetDevInfo(devAssign)))
                    {
                        Entity = entity,
                        User = devAssign==null?null:devAssign.GeneralUser
                    };
                }
            }
        }

        private string GetDevInfo(UIAssignment devAssign)
        {
            if (devAssign == null) return string.Empty;
            return ", developer: " + devAssign.GeneralUser;
        }
    }
}
